// indexviews/hospital/hospital.js
var alert = require("../../utils/alert.js");
var filePath = require("../../utils/servicePath.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hospitalList:[],
    ksList:[],
    hospitalIndex: 0,
    ksIndex:0,  
    //住院时间
    date: "",

    //住院原因
    why:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadHospital();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },
  loadHospital:function(){
    var _this = this;
    wx.request({
      url: filePath.HospitalService(),
      method: "GET",
      success: function (res) {
        _this.setData({
          hospitalList: res.data.data
        })
        _this.loadKs(_this.data.hospitalList[0].value);
      }
    })
  
  },
  loadKs:function(id){
    var _this = this;
    wx.request({
      url: filePath.SectionService() + "?hosbh="+id,
      method: "GET",
      success: function (res) {
        _this.setData({
          ksList: res.data.data
        })
      }
    })
  },
  bindHospitalChange:function(e){
    this.setData({
      hospitalIndex:e.detail.value
    })
  },
  bindKsChange: function (e) {
    this.setData({
      ksIndex: e.detail.value
    })
  },
  bindTypeChange: function (e) {
    this.setData({
      typeIndex: e.detail.value
    })
  },
  //日期变化
  bindDateChange:function(e){
    this.setData({
      date: e.detail.value
    })
  },
  bindWhyInput:function(e){
    this.setData({
      why: e.detail.value
    })
  },
  submitHospital:function(){
    var hospitalIndex = this.data.hospitalIndex;
    if (hospitalIndex < 0) {
      alert.toast("请选择医院");
      return;
    }
    var ksIndex = this.data.ksIndex;
    if (ksIndex < 0) {
      alert.toast("请选择科室");
      return;
    }
    let date = this.data.date;
    if (date == null || date == ''){
      alert.toast("请选择住院日期");
      return;
    }
    let why = this.data.why;
    if (why == null || why == ''){
      alert.toast("请填写申请原因");
      return;
    }
    let url = getApp().globalData.apiDomain + "/BookHos";

    var postValue = {
      AP1201: getApp().globalData.backUserInfo.syS0100,
      AP1203: this.data.hospitalList[hospitalIndex].value,
      AP1204: this.data.hospitalList[hospitalIndex].text,
      AP1205: this.data.ksList[ksIndex].value,
      AP1206: this.data.ksList[ksIndex].text,
      AP1207: this.data.date,
      AP1209: this.data.why
    }
    wx.request({
      url: url,
      method:"POST",
      data: postValue,
      success:function(res){
        console.log("********代办住院********");
        console.log(postValue);
       if(res.data.code == 200) {
         wx.showModal({
           content: '预约成功，办理成功后会以短信形式通知您！',
           showCancel: false,
           success: function (res) {
              wx.navigateBack({
                
              });
           }
         });
       }
      }
    })
  }
})